# COMPARE_matrix_average_Plot.R
# 
# Author: yaping
# Sep 16, 2013
# 5:49:43 PM
###############################################################################


for (e in commandArgs(TRUE)) {
	ta = strsplit(e,"=",fixed=TRUE)
	if(! is.na(ta[[1]][2])) {
		if(ta[[1]][1] == "wd"){
			wd<-ta[[1]][2]  ## directory to make plot file
			
		}
		if(ta[[1]][1] == "output"){
			output<-ta[[1]][2]
		}
		if(ta[[1]][1] == "input"){
			input<- ta[[1]][2]
		}
	}
}

setwd(wd)
data<-read.table(input,sep=" ",header=F)
scale=1000
step=20
bin_size_align=1
colors=c("black")
line_types=1

dataSeq<-seq(((length(data[1,]))/2)-as.integer(scale/bin_size_align), ((length(data[1,]))/2)+as.integer(scale/bin_size_align), by=as.integer(step/bin_size_align))
axisSeq<-seq(0-scale, scale, by=step)
valueGch1<-NULL

for(i in dataSeq){
	if(floor(i+step/(2*bin_size_align)-1)-ceiling(i-step/(2*bin_size_align)) <= 0){
		valueGch1<-cbind(valueGch1,mean(data[,ceiling(i-step/(2*bin_size_align))], na.rm=T))	
			
	}else{
		valueGch1<-cbind(valueGch1,mean(colMeans(data[,ceiling(i-step/(2*bin_size_align)):floor(i+step/(2*bin_size_align)-1)], na.rm=T), na.rm=T))
			
	}
		
}
	pdf(output, paper="special", height=4, width=4)
	par(oma=c(1, 1, 1, 1))
	par(mar=c(1, 1, 3, 1))
	plot(axisSeq,valueGch1,type="l",axes=FALSE,xlab="",ylab="",ylim=c(0,1),col=colors[1],lty=line_types[1],font=2,lwd=3)
	
	axis(2,at=seq(0,1,by=0.2),labels=format(seq(0,1,by=0.2),digits=3),lty=1,font=2,cex.axis=1.0,cex.lab=1.0,font.lab=2,lwd=2)
	axis(1,at=seq(0-scale, scale, by=scale/2),lty=1,font=2,cex.axis=1.2,cex.lab=1.2,font.lab=2,lwd=2)
	mainTitle=paste("numElemCenterToAlign:",length(data[,1]))
	title(mainTitle, cex.main = 0.6, font.main= 4, col.main= "black",xlab="Distance to elements (bp)")
	
	abline(v=0)
	dev.off()


